This question analyses the stock performane of 14 big tech companies in dataset 1 and examines Apple Inc.’s stock (AAPL) as a potential long-term investment and assesses the inherent risks in its short-term fluctuations. The dataset crucial for this analysis includes AAPL’s historical price trends, daily returns, and trading volumes, as detailed in the “AAPL_stock_data.csv”. Understanding these aspects is fundamental in evaluating the stock’s performance and market behavior over time. The interest in this question arises from Apple’s significant role in the tech sector and its impact on investment portfolios, making it a focal point for both individual and institutional investors.
Stock Performance: Investigating AAPL’s historical price trends, daily returns, and trading volumes provides a thorough understanding of its performance. Recognizing patterns of growth and volatility is key to forecasting the stock’s future movements.
Market Behavior: The behavior of AAPL in the market can reveal insights into broader economic impacts, sector trends, and investor sentiments. It’s important to grasp how external factors such as technological advancements, regulatory changes, and economic fluctuations affect AAPL.
Sector Influence: As a leading entity in the tech industry, Apple’s stock movements often mirror wider sector trends and can significantly sway market dynamics. This makes AAPL a key indicator of the health of the technology sector.
Investment Impact: Apple’s significant market capitalization means it plays a major role in many indices and investment funds, including the S&P 500 and various tech-focused portfolios. Its performance can dramatically influence these broader investment vehicles, making its analysis crucial for both individual and institutional investors.
Focal Point for Investors: Given its size, influence, and performance, AAPL is at the center of investment strategies for a diverse group of market participants. Both individual investors and large institutions monitor Apple’s financial health closely to inform their portfolio decisions and risk management strategies.
By exploring these aspects, investors and analysts can make informed decisions, strategically aligning their investment approaches with the opportunities and challenges presented by AAPL’s presence in the market. ## Plotting the stock performance of 14 big tech companies
# Replace the path with the actual path to your CSV file
file_path <- "data/big_tech_stock_prices.csv"
# Read the CSV file into a data frame
data <- read.csv(file_path)
stock_data <- data
# Convert the 'date' column to an actual Date type if it's not already
stock_data$date <- as.Date(stock_data$date)
# Extract year from date
stock_data$year <- format(stock_data$date, "%Y")
# Group data by stock_symbol and year, and then summarize
annual_stock_summary <- stock_data %>%
group_by(stock_symbol, year) %>%
summarise(
average_high = mean(high, na.rm = TRUE),
average_low = mean(low, na.rm = TRUE),
average_open = mean(open, na.rm = TRUE),
average_close = mean(close, na.rm = TRUE),
total_vol = sum(volume, na.rm = TRUE)
) %>%
ungroup()
## `summarise()` has grouped output by 'stock_symbol'. You can override using the
## `.groups` argument.
annual_stock_summary$year <- as.integer(annual_stock_summary$year)
p <- ggplot(
annual_stock_summary,
aes(x = average_high, y=average_high, size = total_vol, colour = stock_symbol)
) +
geom_point(show.legend = c(size = FALSE, color = TRUE), alpha = 0.7) +
scale_color_viridis_d(option = "D") +
scale_size(range = c(1, 28)) +
scale_x_log10() +
labs(x = "Average High",
y = "Average Low",
title = "Average High vs. Low Stock Prices for Big Tech Companies",
subtitle = "Size based on total traded volume",
legend = "Stock Symbol") +
theme_minimal() +
theme(legend.position = "right")
p
Key findings From the animated data visualization, we can see that Apple possessed the biggest volume of traded stock in the market, this motivates us to look deeper into this company to anaylyze its success in the stock market. Another insight is that the volumes of stock traded decreased from 2019, which makes us think that this has something related to the COVID-19 and is the inspiration behind our second questions.
The reason for us to choose
# Replace the path with the actual path to your CSV file
file_path <- "data/big_tech_stock_prices.csv"
# Read the CSV file into a data frame
data <- read.csv(file_path)
# View the first few rows of the data frame
head(data)
## stock_symbol date open high low close adj_close
## 1 AAPL 2010-01-04 7.622500 7.660714 7.585000 7.643214 6.515213
## 2 AAPL 2010-01-05 7.664286 7.699643 7.616071 7.656429 6.526476
## 3 AAPL 2010-01-06 7.656429 7.686786 7.526786 7.534643 6.422664
## 4 AAPL 2010-01-07 7.562500 7.571429 7.466071 7.520714 6.410790
## 5 AAPL 2010-01-08 7.510714 7.571429 7.466429 7.570714 6.453412
## 6 AAPL 2010-01-11 7.600000 7.607143 7.444643 7.503929 6.396483
## volume
## 1 493729600
## 2 601904800
## 3 552160000
## 4 477131200
## 5 447610800
## 6 462229600
# Summing up the volume for each stock
volume_sum <- data %>%
group_by(stock_symbol) %>%
summarise(TotalVolume = sum(volume)) %>%
arrange(desc(TotalVolume))
# Display the summed volumes
print(volume_sum)
## # A tibble: 14 × 2
## stock_symbol TotalVolume
## <chr> <dbl>
## 1 AAPL 838440829600
## 2 TSLA 294389834000
## 3 AMZN 288960091100
## 4 GOOGL 196869939896
## 5 NVDA 166186840100
## 6 MSFT 124351871200
## 7 INTC 117990516000
## 8 CSCO 106950448600
## 9 META 83806858300
## 10 NFLX 60234987900
## 11 ORCL 58938705200
## 12 CRM 22605794200
## 13 IBM 16474538383
## 14 ADBE 12476695200
# Get the top 3 stock symbols
top_3_stocks <- head(volume_sum$stock_symbol, 3)
# Print the top 3 stock symbols
print(top_3_stocks)
## [1] "AAPL" "TSLA" "AMZN"
# Loop through each top stock and save its data to a CSV file
for (stock in top_3_stocks) {
stock_data <- data %>%
filter(stock_symbol == stock) %>%
select(-stock_symbol)
# Construct file name and path
file_name <- paste0(stock, "_stock_data.csv")
file_path <- file.path("./", file_name) # Replace with your actual directory path
# Save to CSV
write.csv(stock_data, file_path, row.names = FALSE)
# Print out file path for confirmation
print(paste0("Data for ", stock, " saved to: ", file_path))
}
## [1] "Data for AAPL saved to: .//AAPL_stock_data.csv"
## [1] "Data for TSLA saved to: .//TSLA_stock_data.csv"
## [1] "Data for AMZN saved to: .//AMZN_stock_data.csv"
# Load AAPL stock data
aapl_data <- read.csv("data/customed/AAPL_stock_data.csv")
# Convert date column to Date type
aapl_data$date <- as.Date(aapl_data$date)
# Display the structure and summary of the dataset
str(aapl_data)
## 'data.frame': 3271 obs. of 7 variables:
## $ date : Date, format: "2010-01-04" "2010-01-05" ...
## $ open : num 7.62 7.66 7.66 7.56 7.51 ...
## $ high : num 7.66 7.7 7.69 7.57 7.57 ...
## $ low : num 7.58 7.62 7.53 7.47 7.47 ...
## $ close : num 7.64 7.66 7.53 7.52 7.57 ...
## $ adj_close: num 6.52 6.53 6.42 6.41 6.45 ...
## $ volume : int 493729600 601904800 552160000 477131200 447610800 462229600 594459600 605892000 432894000 594067600 ...
summary(aapl_data)
## date open high low
## Min. :2010-01-04 Min. : 6.87 Min. : 7.00 Min. : 6.795
## 1st Qu.:2013-04-04 1st Qu.: 18.97 1st Qu.: 19.12 1st Qu.: 18.780
## Median :2016-07-01 Median : 29.75 Median : 29.98 Median : 29.555
## Mean :2016-07-01 Mean : 51.27 Mean : 51.85 Mean : 50.709
## 3rd Qu.:2019-10-01 3rd Qu.: 56.90 3rd Qu.: 57.26 3rd Qu.: 56.435
## Max. :2022-12-29 Max. :182.63 Max. :182.94 Max. :179.120
## close adj_close volume
## Min. : 6.859 Min. : 5.847 Min. :3.520e+07
## 1st Qu.: 18.966 1st Qu.: 16.626 1st Qu.:1.024e+08
## Median : 29.812 Median : 27.385 Median :1.667e+08
## Mean : 51.297 Mean : 49.445 Mean :2.563e+08
## 3rd Qu.: 56.761 3rd Qu.: 54.876 3rd Qu.:3.458e+08
## Max. :182.010 Max. :180.960 Max. :1.881e+09
# Check for null values
sum(is.na(aapl_data))
## [1] 0
# Determine the global minimum low and maximum high values
global_min <- min(aapl_data$low, na.rm = TRUE)
global_max <- max(aapl_data$high, na.rm = TRUE)
fig <- aapl_data %>% plot_ly(x = ~date, type="candlestick",
open = ~open, close = ~close,
high = ~high, low = ~low)
fig <- fig %>% layout(title = "AAPL Stock Candlestick Chart",
xaxis = list(
type = "date",
rangeslider = list(visible = F),
rangeselector = list(
buttons = list(
list(count = 10, label = "10Y", step = "year", stepmode = "backward"),
list(count = 5, label = "5Y", step = "year", stepmode = "backward"),
list(count = 1, label = "1Y", step = "year", stepmode = "backward"),
list(count = 1, label = "1M", step = "month", stepmode = "backward"),
list(count = 1, label = "1W", step = "week", stepmode = "backward"),
list(step = "all")
)
)
)) %>%
config(displayModeBar = TRUE, displaylogo = FALSE, modeBarButtonsToRemove =
c("autoScale2d", "toggleSpikelines", "zoomIn2d", "zoomOut2d", "select2d", "lasso2d"))
fig
# Plotting the adjusted close price over time
ggplot(aapl_data, aes(x = date, y = adj_close)) +
geom_line() +
theme_minimal() +
labs(title = "AAPL Stock Adjusted Close Price Over Time",
x = "Date", y = "Adjusted Close Price")
The Adjusted Close Price graph of AAPL underscores a compelling growth narrative, supported by a comprehensive dataset spanning 3,271 trading days. This extensive period allows for a detailed analysis of the stock’s performance, showcasing a notable upward trend indicative of strong market confidence and Apple’s solid fundamentals.
# Calculating daily returns
aapl_data <- aapl_data %>%
arrange(date) %>%
mutate(daily_return = (adj_close / lag(adj_close) - 1) * 100)
# Plotting daily returns
ggplot(aapl_data, aes(x = date, y = daily_return)) +
geom_line() +
theme_minimal() +
labs(title = "Daily Returns of AAPL Stock",
x = "Date", y = "Daily Return (%)")
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_line()`).
library(ggplot2)
library(dplyr)
library(plotly)
# Assuming 'aapl_data' already has 'daily_return' calculated and is ordered by date
# Calculate mean and standard deviation
mean_return <- mean(aapl_data$daily_return, na.rm = TRUE)
std_dev <- sd(aapl_data$daily_return, na.rm = TRUE)
# Create a ggplot histogram
p <- ggplot(aapl_data, aes(x = daily_return)) +
geom_histogram(binwidth = 0.1, fill = "darkblue", color = "white") +
geom_vline(xintercept = mean_return, color = "red", linetype = "dashed", size = 0.2) +
geom_vline(xintercept = mean_return + std_dev, color = "red", linetype = "solid", size = 0.2) +
geom_vline(xintercept = mean_return - std_dev, color = "red", linetype = "solid", size = 0.2) +
labs(title = "Histogram of Daily Returns of AAPL Stock",
x = "Daily Return (%)", y = "Frequency") +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Convert the ggplot object to a plotly object
fig <- ggplotly(p)
## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_bin()`).
# Add hover text for standard deviation lines
fig <- fig %>%
layout(annotations = list(
list(x = mean_return - 1, y = 50, text = "Standard Deviation", showarrow = TRUE, arrowhead = 2, ax = -90)
))
fig
The Daily Returns graph provides a clear depiction of AAPL’s volatility, characterized by fluctuations around a central tendency with occasional spikes. This volatility is a testament to the dynamic nature of the stock market and Apple’s sensitivity to market sentiments and news.
# Defining trend categories based on daily returns
aapl_data$trend <- with(aapl_data, case_when(
daily_return <= 0.5 & daily_return >= -0.5 ~ "Slight or No change",
daily_return > 0.5 & daily_return <= 1 ~ "Slight Positive",
daily_return < -0.5 & daily_return >= -1 ~ "Slight Negative",
daily_return > 1 & daily_return <= 5 ~ "Positive",
daily_return < -1 & daily_return >= -5 ~ "Negative",
daily_return > 5 ~ "Extremely Positive",
daily_return < -5 ~ "Extremely Negative"
))
# Plotting trend distribution
aapl_data %>%
group_by(trend) %>%
summarise(count = n()) %>%
ggplot(aes(x = reorder(trend, -count), y = count, fill = trend)) +
geom_bar(stat = "identity") +
theme_minimal() +
labs(title = "Trend Distribution of AAPL Daily Returns",
x = "Trend", y = "Count") +
coord_flip()
The trend distribution offers a nuanced view of AAPL’s daily return patterns, categorizing days into various trends based on the magnitude of price changes. This segmentation reveals the predominance of days with ‘Slight or No change’, highlighting an underlying stability amidst volatility. Moreover, comparing between positive and negative of each type, we can clearly see that positive out perform negative in each segmentation, which will ultimately translate into profit in long term.
The time series plot shows a general upward trend in AAPL’s stock price, supporting its consideration as a viable long-term investment. However, this plot might also reveal periods of volatility during market downturns or economic uncertainty, highlighting potential risks that need to be managed.
The histogram of daily returns will likely demonstrate AAPL’s short-term trading risks. A wide spread in the distribution of returns, especially if skewing towards negative outcomes, could indicate higher volatility, which is often seen in tech stocks influenced by market sentiments and external economic factors.
Together, these plots will provide a dual perspective on AAPL’s investment profile. If the long-term trend is strong but accompanied by significant short-term fluctuations, the stock may be more suited to investors who can tolerate volatility or those employing strategies that leverage these movements. Conversely, a more stable long-term trend accompanied by moderate daily fluctuations would appeal to conservative investors seeking growth with manageable risks.
By analyzing these trends, investors and analysts can better understand the conditions under which AAPL thrives and the potential challenges it faces, facilitating more informed decision-making regarding portfolio inclusion and investment strategy.